﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Data.SQLite;
using System.Windows.Forms;
using System.IO;

namespace MainFrame
{
    public delegate void _messagedisplay(string message);

    public class sqlHelperDapper
    {
        IDbConnection _connection;
        string m_tableName;
        public  _messagedisplay MessageDisplay;
        string m_dbName;
        string m_userName;
        string m_password;
        string m_ip;
        dbType m_dbtype;
        public string TableName
        {
            get { return m_tableName; }
            set { m_tableName = value; }
        }
        public sqlHelperDapper(dbType dbtype, string dbName, string userName, string password, string ip)
        {
            _connection=  ConnectString(dbtype, dbName, userName, password, ip);
            m_dbtype = dbtype;
            m_dbName = dbName;
            m_userName = userName;
            m_password = password;
            m_ip = ip;
        }
    
 
        /// <summary>
        /// 获取不同数据库的connection
        /// </summary>
        /// <param name="dbtype">数据库类型（sqlserver,mysql,sqlite,oracle）</param>
        /// <param name="dbName">数据库名称，当为sqlite时，为数据库的存放路径</param>
        /// <param name="userName">数据库访问用户名，如果没有，为空</param>
        /// <param name="password">数据库访问密码，如果没有，为空</param>
        /// <param name="ip">数据库所在的IP地址</param>
        /// <returns>返回connection</returns>
        private IDbConnection ConnectString(dbType dbtype, string dbName,string userName,string password,string ip)
        {
            try
            {
                string connString = "";
                IDbConnection connection = null;
                switch (dbtype)
                {
                    case dbType.sqlserver:
                        connString = "server=" + ip + ";database=" + dbName + ";User=" + userName + ";password=" + password + ";Connect Timeout=1000000";
                        connection = new SqlConnection(connString);

                        break;
                    case dbType.mysql:
                        connString = "Database=" + dbName + ";Data Source=" + ip + ";User Id=" + userName + ";Password=" + password + ";CharSet=utf8;port=3306";
                        connection = new MySqlConnection(connString);
                        break;
                    case dbType.sqlite:
                        SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
                        sb.DataSource = dbName;//此处dbName,为数据库的存放路径（含数据库名称及扩展名）
                        connection = new SQLiteConnection(sb.ToString());
                        //connString = "Data Source=" + dbName;
                        break;
                    case dbType.oracle:
                        //connString = "DATA SOURCE="+ip+";PASSWORD="+password+";PERSIST SECURITY INFO=True;USER ID="+userName ;
                        connString = "DATA SOURCE=" + ip + ";PASSWORD=" + password + ";PERSIST SECURITY INFO=True;USER ID=" + userName;
                        break;
                    default:
                        MessageDisplay("请选择合适的数据库类型!");
                        break;
                }
                return connection;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("ConnectString 异常：" + ex.Message);
                return null;
            }
            
        }

        /// <summary>
        /// 获取本地数据库的connection
        /// </summary>
        /// <param name="dbtype">数据库类型（sqlserver,mysql,sqlite,oracle）</param>
        /// <param name="dbName">数据库名称，当为sqlite时，为数据库的存放路径</param>
        /// <param name="userName">数据库访问用户名，如果没有，为空</param>
        /// <param name="password">数据库访问密码，如果没有，为空</param>
        /// <returns>返回connection</returns>
        private IDbConnection ConnectString(dbType dbtype, string dbName, string userName, string password)
        {
            try
            {
                string connString = "";
                IDbConnection connection = null;
                switch (dbtype)
                {
                    case dbType.sqlserver:
                        connString = "server=127.0.0.1;database=" + dbName + ";User=" + userName + ";password=" + password + ";Connect Timeout=1000000";
                        connection = new SqlConnection(connString);
                        break;
                    case dbType.mysql:
                        connString = "Database=" + dbName + ";Data Source=localhost;User Id=" + userName + ";Password=" + password + ";CharSet=utf8;port=3306";
                        connection = new MySqlConnection(connString);
                        break;
                    case dbType.sqlite:
                        SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
                        sb.DataSource = dbName;//此处dbName,为数据库的存放路径（含数据库名称及扩展名）
                        connection = new SQLiteConnection(sb.ToString());
                        //connString = "Data Source=" + dbName;
                        break;
                    case dbType.oracle:
                        //connString = "DATA SOURCE="+ip+";PASSWORD="+password+";PERSIST SECURITY INFO=True;USER ID="+userName ;
                        connString = "DATA SOURCE=localhost;PASSWORD=" + password + ";PERSIST SECURITY INFO=True;USER ID=" + userName;
                        break;
                    default:
                        break;
                }
                return connection;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("ConnectString 异常：" + ex.Message);
                return null;
            }
            
        }
        //private int CreatDB()
        //{
        //    设备状态记录
        //}
        /// <summary>
        /// 表示一个与数据源的开放连接
        /// </summary>
        /// <returns></returns>
        public IDbConnection ConnectString()
        {
            try
            {
                string connString = "";
                //IDbConnection connection = null;
                switch (m_dbtype)
                {
                    case dbType.sqlserver://m_ip--IP地址，m_dbName--数据库名字，m_userName--用户名，m_password--用户密码，
                        connString = "server=" + m_ip + ";database=" + m_dbName + ";User=" + m_userName + ";password=" + m_password + ";Connect Timeout=1000000";
                        _connection = new SqlConnection(connString);//用于打开 SQL Server 数据库的连接。查出来的数据存到_connection里，_connection有1000行（应该是这个意思）
                        _connection.Open();
                        break;
                    case dbType.mysql:
                        connString = "Database=" + m_dbName + ";Data Source=" + m_ip + ";User Id=" + m_userName + ";Password=" + m_password + ";CharSet=utf8;port=3306";
                        _connection = new MySqlConnection(connString);

                        break;
                    case dbType.sqlite:
                        SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
                        sb.DataSource = m_dbName;//此处dbName,为数据库的存放路径（含数据库名称及扩展名）
                        _connection = new SQLiteConnection(sb.ToString());
                        //connString = "Data Source=" + dbName;
                        break;
                    case dbType.oracle:
                        //connString = "DATA SOURCE="+ip+";PASSWORD="+password+";PERSIST SECURITY INFO=True;USER ID="+userName ;
                        connString = "DATA SOURCE=" + m_ip + ";PASSWORD=" + m_password + ";PERSIST SECURITY INFO=True;USER ID=" + m_userName;
                        break;
                    default:
                        MessageDisplay("请选择合适的数据库类型!");
                        break;
                }
                return _connection;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("ConnectString 异常：" + ex.Message);
                return null;
            }

        }
        public IDbConnection ConnectStringLocal()
        {
            try
            {
                string connString = "";
                //IDbConnection connection = null;
                switch (m_dbtype)
                {
                    case dbType.sqlserver:
                        connString = "server=127.0.0.1;database=" + m_dbName + ";User=" + m_userName + ";password=" + m_password + ";Connect Timeout=1000000";
                        _connection = new SqlConnection(connString);
                        break;
                    case dbType.mysql:
                        connString = "Database=" + m_dbName + ";Data Source=localhost;User Id=" + m_userName + ";Password=" + m_password + ";CharSet=utf8;port=3306";
                        _connection = new MySqlConnection(connString);
                        break;
                    case dbType.sqlite:
                        SQLiteConnectionStringBuilder sb = new SQLiteConnectionStringBuilder();
                        sb.DataSource = m_dbName;//此处dbName,为数据库的存放路径（含数据库名称及扩展名）
                        _connection = new SQLiteConnection(sb.ToString());
                        //connString = "Data Source=" + dbName;
                        break;
                    case dbType.oracle:
                        //connString = "DATA SOURCE="+ip+";PASSWORD="+password+";PERSIST SECURITY INFO=True;USER ID="+userName ;
                        connString = "DATA SOURCE=localhost;PASSWORD=" + m_password + ";PERSIST SECURITY INFO=True;USER ID=" + m_userName;
                        break;
                    default:
                        MessageDisplay("请选择合适的数据库类型!");
                        break;
                }
                return _connection;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("ConnectString 异常：" + ex.Message);
                return null;
            }

        }
        public List<T> selectFullTable<T>(string tableName)
        {
            try
            {
                //SELECT ID, NAME, SALARY FROM COMPANY; //只获取指定的字段内容（items）,items可以为空
                //SELECT sql FROM sqlite_master WHERE type = 'table' AND tbl_name = 'COMPANY';
                // colNames长度、colValues长度、operateions长度一样
                //string text = "SELECT * FROM " + tableName + " WHERE " + colName + " " + operation + " '" + colValue + "'";
                if (string.IsNullOrEmpty(tableName))
                {
                    MessageDisplay("表名不能为空！！！");
                    return null;
                }
                ConnectString();
                List<T> ret = new List<T>();
                _connection.Open();
                
                    string sqlCommandText = @"SELECT * FROM "+tableName;
                    ret = _connection.Query<T>(sqlCommandText).ToList();
                _connection.Close();
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("selectFullTable 异常：" + ex.Message);
                return null;
            }
        }
        public List<T> selectFullTable<T>()
        {
            try
            {
                if (string.IsNullOrEmpty(m_tableName))
                {
                   System.Windows.Forms. MessageBox.Show("表名不能为空");
                    return null;
                }
                List<T> ret = new List<T>();
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"SELECT * FROM " + m_tableName;
                    ret = conn.Query<T>(sqlCommandText).ToList();
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 查询前num个数据行
        /// </summary>
        /// <typeparam name="T">泛型数据类型，通常为自定义的类，类中的数据对应数据库表的列</typeparam>
        /// <param name="tableName">要查询的表名</param>
        /// <param name="num">查询前num条数据</param>
        /// <returns>返回的 List<T>数据</returns>
        public List<T> selectTopTable<T>(string tableName,int num)
        {
            try
            {
                if (string.IsNullOrEmpty( tableName)||num==0)
                {
                    return null;
                }
                List<T> ret = new List<T>();
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"SELECT TOP "+num.ToString()+" * FROM " + tableName;
                    ret = conn.Query<T>(sqlCommandText).ToList();
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 查询前num个数据行（数据表在初始化或者使用前指定）
        /// </summary>
        /// <typeparam name="T">泛型数据类型，通常为自定义的类，类中的数据对应数据库表的列</typeparam>
        /// <param name="num">查询前num条数据</param>
        /// <returns>返回的 List<T>数据</returns>
        public List<T> selectTopTable<T>(int num)
        {
            try
            {
                if (string.IsNullOrEmpty(m_tableName))
                {
                    MessageDisplay("表名不能为空!");
                    return null;
                }
                List<T> ret = new List<T>();
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"SELECT TOP "+num.ToString()+" * FROM " + m_tableName;
                    ret = conn.Query<T>(sqlCommandText).ToList();
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("selectTopTable 异常:"+ex.Message);
                return null;
            }
        }
        /// <summary>
        /// 根据输入的查询语句查询（要求熟悉查询语法）
        /// </summary>
        /// <typeparam name="T">泛型数据，通常是自定义数据类</typeparam>
        /// <param name="sql">查询语句</param>
        /// <returns>返回的查询结果</returns>
        public List<T> select<T>(string sql)
        {
            try
            {
                if (string.IsNullOrEmpty(sql))
                {
                    MessageDisplay("查询语句不能为空");
                    return null;
                }
                List<T> ret = new List<T>();
                displalyInfo.displayInfoMain(sql);
                _connection.Open();
                 ret = _connection.Query<T>(sql).ToList();
                _connection.Close();
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("select 异常：" + ex.Message);
                _connection.Close();
                return null;
            }
        }

        /// <summary>
        /// 自定义查询语句（Where之后的语句）
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要查询的数据库表名称</param>
        /// <param name="sql_WhereAfter">自定义的where之后的语句，字符串、时间日期要加单引号</param>
        /// <returns></returns>
        public List<T> select<T>(string tableName,string sql_WhereAfter)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName)|| string.IsNullOrEmpty(sql_WhereAfter))
                {
                    MessageDisplay("表名或者查询语句不能为空！！！");
                    return null;
                }
                List<T> ret = new List<T>();
                using (IDbConnection conn = _connection)//执行完程序块后，using里的资源会自动释放掉(conn)
                {
                    //“2020”附近有语法错误。
                    //字符串 '' 后的引号不完整。
                    string sqlCommandText = @"SELECT * FROM " + tableName+" Where "+sql_WhereAfter;
                    //select查询列表第一列的名字，*代表第一列的所有 from整个表格的名字 where筛选条件
                    //ConnectString();
                    ret = conn.Query<T>(sqlCommandText).ToList();

                    //selectStr = "select * from Int_LotProductInfo order by oid desc"
                    //selectStr = "select * from Int_LotProductInfo where  order by oid desc"
                    //SELECT C2OrderNo,Remark1,Remark2 FROM PlanTable where C2OrderNo  =  'lot_BoxOrderNo'
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("select 异常：" + ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 自定义删除语句（Where之后的语句）
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要删除的数据库表名称</param>
        /// <param name="sql_WhereAfter">自定义的where之后的语句，字符串、时间日期要加单引号</param>
        /// <returns></returns>
        public int delete(string tableName, string sql_WhereAfter)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(sql_WhereAfter))
                {
                    MessageDisplay("表名或者查询语句不能为空！！！");
                    return -1;
                }
                int ret = 0;
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"DELETE FROM " + tableName + " Where " + sql_WhereAfter;
                    ret = conn.Execute(sqlCommandText);
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("delete 异常：" + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 删除表中所有内容
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要删除的数据库表名称</param>
        /// <returns></returns>
        public int delete(string tableName)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName))
                {
                    MessageDisplay("表名或者查询语句不能为空！！！");
                    return -1;
                }
                int ret = 0;
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"DELETE FROM " + tableName ;
                    ret = conn.Execute(sqlCommandText);
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("delete 异常：" + ex.Message);
                return -1;
            }
        }
        /// <summary>
        /// 删除之后查询语句（Where之后的语句）
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要删除查询的数据库表名称</param>
        /// <param name="sql_WhereAfter">自定义的where之后的语句，字符串、时间日期要加单引号</param>
        /// <returns></returns>
        public List<T> deleteAfterSelect<T>(string tableName, string sql_WhereAfter)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(sql_WhereAfter))
                {
                    MessageDisplay("表名或者查询语句不能为空！！！");
                    return null;
                }
                List<T> ret = new List<T>();
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"DELETE FROM " + tableName + " Where " + sql_WhereAfter;
                    conn.Execute(sqlCommandText);
                    string sqlCommandText1 = @"SELECT * FROM " + tableName;
                    ret = conn.Query<T>(sqlCommandText1).ToList();
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("deleteAfterSelect 异常：" + ex.Message);
                return null;
            }
        }
        /// <summary>
        /// 更新之后查询语句（Where之后的语句）
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要更新查询的数据库表名称</param>
        /// <param name="updateString">要更新列名称及对应值，以逗号隔开</param>
        /// <param name="sql_WhereAfter">自定义的where之后的语句，字符串、时间日期要加单引号</param>
        /// <returns></returns>
        public List<T> updateAfterSelect<T>(string tableName,string updateString, string sql_WhereAfter)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(sql_WhereAfter) || string.IsNullOrEmpty(updateString))
                {
                    MessageDisplay("表名或者查询语句不能为空！！！");
                    return null;
                }
                List<T> ret = new List<T>();
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"UPDATE " + tableName +" SET "+updateString+ " Where " + sql_WhereAfter;
                    conn.Execute(sqlCommandText);
                    string sqlCommandText1 = @"SELECT * FROM " + tableName;
                    ret = conn.Query<T>(sqlCommandText1).ToList();
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("updateAfterSelect 异常：" + ex.Message);
                return null;
            }
        }
        /// <summary>
        /// 更新语句（Where之后的语句）
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要更新查询的数据库表名称</param>
        /// <param name="updateString">要更新列名称及对应值，以逗号隔开</param>
        /// <param name="sql_WhereAfter">自定义的where之后的语句，字符串、时间日期要加单引号</param>
        /// <returns></returns>
        public int update(string tableName, string updateString, string sql_WhereAfter)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(sql_WhereAfter)|| string.IsNullOrEmpty(updateString))
                {
                    MessageDisplay("表名或者更新字符或者查询语句不能为空！！！");
                    return -1;
                }
                int ret = 0;
          
                    string sqlCommandText = @"UPDATE " + tableName + " SET " + updateString + " Where " + sql_WhereAfter;
                MessageDisplay(sqlCommandText);
                _connection.Open();
                ret = _connection.Execute(sqlCommandText);
                _connection.Close();
                Task.Run(() => saveSql(sqlCommandText));
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("update 异常：" + ex.Message);
                return -1;
            }
        }
        private void saveSql(string sqlCommandText)
        {
            try
            {
                string path_Save = Application.StartupPath + "\\UPDATE";
                if (!Directory.Exists(path_Save))
                {
                    Directory.CreateDirectory(path_Save);
                }
                string Pathstr = path_Save + "\\" + DateTime.Now.ToString("yyyy-MM") + ".txt";
                FileStream fs;
                if (!File.Exists(Pathstr))
                    fs = new FileStream(Pathstr, FileMode.OpenOrCreate);
                else
                    fs = new FileStream(Pathstr, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "  " + sqlCommandText);
                sw.Close();
                fs.Close();
            }
            catch(Exception ex)
            {
                MessageDisplay(ex.Message);
            }
           
        }
        /// <summary>
        /// 插入且查询语句
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要插入查询的数据库表名称</param>
        /// <param name="columnStr">要更新数据表中列名称，以逗号隔开</param>
        /// <param name="Tname">t中数据对应名称，以@+t数据+,形式</param>
        /// <param name="t">泛型数据，传入自定义的类实例</param>
        /// <returns>插入查询成功，查到的数据</returns>
        public List<T> InsertAfterSelect<T>(string tableName, string columnStr, string Tname,T t)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(Tname) || string.IsNullOrEmpty(columnStr))
                {
                    MessageDisplay("表名或者查询语句不能为空！！！");
                    return null;
                }
                List<T> ret = new List<T>();
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"INSERT INTO " + tableName + " (" + columnStr + ") VALUES (" + Tname + ")";
                    conn.Execute(sqlCommandText,t);
                    string sqlCommandText1 = @"SELECT * FROM " + tableName;
                    ret = conn.Query<T>(sqlCommandText1).ToList();
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("InsertAfterSelect 异常：" + ex.Message);
                return null;
            }
        }
        /// <summary>
        /// 插入语句
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要插入查询的数据库表名称</param>
        /// <param name="columnStr">要更新数据表中列名称，以逗号隔开</param>
        /// <param name="Tname">t中数据对应名称，以@+t数据+,形式</param>
        /// <param name="t">泛型数据，传入自定义的类实例</param>
        /// <returns>插入成功，返回非零值</returns>
      
        public int insert<T>(string tableName, T t)
        {
            try
            {
                if (t != null)
                {
                    var fields = t.GetType().GetProperties();
                    string columnStr = "";
                    var Tname = "";
                    for (int i = 0; i < fields.Length; i++)
                    {
                        columnStr += fields[i].Name + ",";
                        Tname += "@" + fields[i].Name + ",";
                    }
                   
                    if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(columnStr) || string.IsNullOrEmpty(Tname))
                    {
                        displalyInfo.displayInfoMain("表名或者更新字符或者查询语句不能为空！！！");
                        return -1;
                    }
                    int ret = 0;
                    columnStr=columnStr.Remove(columnStr.Length-1, 1);
                    Tname=Tname.Remove(Tname.Length-1, 1);

                    lock (lockobJ)
                    {
                        _connection.Open();
                        string sqlCommandText = @"INSERT INTO " + tableName + " (" + columnStr + ") VALUES (" + Tname + ")";
                        ret = _connection.Execute(sqlCommandText, t);
                        _connection.Close();
                    }
                    return ret;
                }
                else
                    return -1;
               
            }
            catch (System.Exception ex)
            {
                _connection.Close();
                displalyInfo.displayInfoMain("insert 异常：" + ex.Message);
                return -1;
            }
        }
        private object lockobJ = new object();
        public List<T> InsertAfterSelect<T>(string tableName, string columnStr, string Tname, List<T> t)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(Tname) || string.IsNullOrEmpty(columnStr))
                {
                    MessageDisplay("表名或者查询语句不能为空！！！");
                    return null;
                }
                List<T> ret = new List<T>();
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"INSERT INTO " + tableName + " (" + columnStr + ") VALUES (" + Tname + ")";
                    conn.Execute(sqlCommandText, t);
                    string sqlCommandText1 = @"SELECT * FROM " + tableName;
                    ret = conn.Query<T>(sqlCommandText1).ToList();
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("InsertAfterSelect 异常：" + ex.Message);
                return null;
            }
        }
        /// <summary>
        /// 插入语句
        /// </summary>
        /// <typeparam name="T">泛型数据，通常为与数据库表中的列对应的类数据</typeparam>
        /// <param name="tableName">要插入查询的数据库表名称</param>
        /// <param name="columnStr">要更新数据表中列名称，以逗号隔开</param>
        /// <param name="Tname">t中数据对应名称，以@+t数据+,形式</param>
        /// <param name="t">泛型数据，传入自定义的类实例</param>
        /// <returns>插入成功，返回非零值</returns>
        public int insert<T>(string tableName, string columnStr, string Tname, List<T> t)
        {
            try
            {
                if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(columnStr) || string.IsNullOrEmpty(Tname))
                {
                    MessageDisplay("表名或者更新字符或者查询语句不能为空！！！");
                    return -1;
                }
                int ret = 0;
                using (IDbConnection conn = _connection)
                {
                    string sqlCommandText = @"INSERT INTO " + tableName + " (" + columnStr + ") VALUES (" + Tname + ")";
                    ret = conn.Execute(sqlCommandText, t);
                }
                return ret;
            }
            catch (System.Exception ex)
            {
                MessageDisplay("insert 异常：" + ex.Message);
                return -1;
            }
        }
    }
   public  enum dbType
    {
        sqlserver=0,
        mysql,
        sqlite,
        oracle
    }
}
